﻿/*
 * Cratos Desktop
 * Cratos Dual WCF Service Callback class
 * 
 * Copyright (c) 2011-2012 Draco
 * 
 * This file is part of Cratos.
 * 
 * Cratos is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Cratos is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cratos.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *       .==.        .==.
 *      //'^\\      //^'\\
 *     // ^ ^\(\__/)/^ ^^\\
 *    //^ ^^ ^/6  6\ ^^ ^ \\
 *   //^ ^^ ^/( .. )\^ ^ ^ \\
 *  // ^^ ^/\| V""V|/\^ ^ ^ \\
 * //^^/\/ /  '~~'  \ \/\^ ^ \\
 *  ------------------------------------
 *  HERE BE DRAGONS
 *  
 *  Johan Dercksen - johan@draco.co.za
 */
using System;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using Cratos.Tools.General;
using Cratos.Tools.DataObject;
using Cratos.Desktop.CratosDualWcfService;

namespace Cratos.Desktop
{
	/// <summary>
	/// Cratos New Page event delegate declaration
	/// </summary>
	/// <param name="pageNo">Page Number</param>
	/// <param name="doCollection">Data Object Collection</param>
	public delegate void CratosNewPageEventHandler(long pageNo, Collection<IDOCratos> doCollection);

	/// <summary>
	/// Cratos Error event delegate declaration
	/// </summary>
	/// <param name="errorCollection">Error Collection</param>
	public delegate void CratosErrorEventHandler(Collection<CratosError> errorCollection);

	/// <summary>
	/// Cratos Completed event delegate declaration
	/// </summary>
	/// <param name="actionResult">Cratos Action result</param>
	/// <param name="doCollection">Data Object Collection</param>
	public delegate void CratosCompletedEventHandler(DOCratosActionResult actionResult, Collection<IDOCratos> doCollection);

	/// <summary>
	/// Cratos Dual WCF Service Callback class
	/// </summary>
	//[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, UseSynchronizationContext = false)]
	[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
	public class CratosDualWcfServiceCallback : ICratosDualWcfServiceCallback
	{
		#region Class Constant declarations
		#endregion

		#region Class Event declarations
		/// <summary>
		/// Cratos New Page Event
		/// </summary>
		public event CratosNewPageEventHandler NewPage;

		/// <summary>
		/// Cratos Error Event
		/// </summary>
		public event CratosErrorEventHandler Error;

		/// <summary>
		/// Cratos Completed Event
		/// </summary>
		public event CratosCompletedEventHandler Completed;
		#endregion

		#region Class Variable declarations
		#endregion

		#region Class Constructors & Destructors
		#endregion

		#region Class Properties
		#endregion

		#region Class Methods
		#region Cratos New Page Methods
		/// <summary>
		/// Cratos New Page
		/// </summary>
		/// <param name="ctCratos"></param>
		public void CratosNewPage(CratosCollectionNewPageContract ctCratos)
		{
			// Declare the required variables
			Collection<IDOCratos> doCollection;

			// Fire the relevant event if required
			if (this.NewPage != null)
			{
				// Create the incoming Collection
				doCollection = (Collection<IDOCratos>) DOCratosGlobal.BinDeserialize(ctCratos.DOStream);

				// Raise the relevant event
				this.NewPage(ctCratos.PageNo, doCollection);
			}

			// Return to calling method
			return;
		}

		/// <summary>
		/// Begin Cratos Result
		/// </summary>
		/// <param name="ctCratos"></param>
		/// <param name="callback"></param>
		/// <param name="asyncState"></param>
		/// <returns></returns>
		public IAsyncResult BeginCratosNewPage(CratosCollectionNewPageContract ctCratos, AsyncCallback callback, object asyncState)
		{
			throw new NotImplementedException();
		}

		/// <summary>
		/// End Cratos Result
		/// </summary>
		/// <param name="result"></param>
		public void EndCratosNewPage(IAsyncResult result)
		{
			throw new NotImplementedException();
		}
		#endregion

		#region Cratos Error Methods
		/// <summary>
		/// Cratos Error 
		/// </summary>
		/// <param name="ctCratos"></param>
		public void CratosError(CratosCollectionErrorContract ctCratos)
		{
			// Declare the required variables
			Collection<CratosError> errorCollection;

			// Fire the relevant event if required
			if (this.Error != null)
			{
				// Create and Setup the error collection
				errorCollection = new Collection<CratosError>();
				foreach (CratosError currentError in ctCratos.Errors) { errorCollection.Add(currentError); }

				// Raise the relevant event
				this.Error(errorCollection);
			}

			// Return to calling method
			return;
		}

		/// <summary>
		/// Begin Cratos Error
		/// </summary>
		/// <param name="ctCratos"></param>
		/// <param name="callback"></param>
		/// <param name="asyncState"></param>
		/// <returns></returns>
		public IAsyncResult BeginCratosError(CratosCollectionErrorContract ctCratos, AsyncCallback callback, object asyncState)
		{
			throw new NotImplementedException();
		}

		/// <summary>
		/// End Cratos Error
		/// </summary>
		/// <param name="result"></param>
		public void EndCratosError(IAsyncResult result)
		{
			throw new NotImplementedException();
		}
		#endregion

		#region Cratos Completed Methods
		/// <summary>
		/// Cratos Completed
		/// </summary>
		/// <param name="ctCratos"></param>
		public void CratosCompleted(CratosCollectionCompletedContract ctCratos)
		{
			// Declare the required variables
			Collection<IDOCratos> doCollection;

			// Fire the relevant event if required
			if (this.Completed != null)
			{
				// Create the incoming Collection
				doCollection = (Collection<IDOCratos>) DOCratosGlobal.BinDeserialize(ctCratos.DOStream);

				// Raise the relevant event
				this.Completed(ctCratos.ActionResult, doCollection);
			}

			// Return to calling method
			return;
		}

		/// <summary>
		/// Begin Cratos Completed
		/// </summary>
		/// <param name="ctCratos"></param>
		/// <param name="callback"></param>
		/// <param name="asyncState"></param>
		/// <returns></returns>
		public IAsyncResult BeginCratosCompleted(CratosCollectionCompletedContract ctCratos, AsyncCallback callback, object asyncState)
		{
			throw new NotImplementedException();
		}

		/// <summary>
		/// End Cratos Completed
		/// </summary>
		/// <param name="result"></param>
		public void EndCratosCompleted(IAsyncResult result)
		{
			throw new NotImplementedException();
		}
		#endregion
		#endregion

		#region Class Support Methods
		#endregion
	}
}
